< previous page page_55 next page >

Page 55
Before enumerating the subkeys, you should be able to use the RegQueryInfoKey function to obtain the number of keys and the maximum length of the subkey names using the following function:
Private Function GetKeylnfo(ByVal hKey As Long, NumberOfKeys _
As Long, MaxKeyNameLength As Long) As Long
   Dim cbClass As Long
   Dim cSubKeys As Long
   Dim cbMaxSubKeyLen As Long
   Dim cbMaxClassLen As Long
   Dim cValues As Long
   Dim cbMaxValueNameLen As Long
   Dim cbMaxValueLen As Long
   Dim cbSecurityDescriptor As Long
   Dim ftLastWriteTime As FILETIME
   Dim res As Long

   res = RegQueryInfoKey(hKey, vbNullString, cbClass, _
            0, cSubKeys, cbMaxSubKeyLen, cbMaxClassLen, _
            cValues, cbMaxValueNameLen, cbMaxValueLen, _
            cbSecurityDescriptor, ftLastWriteTime)
   If res <> 0 Then
      MsgBox "RegQueryInfoKey error: " & GetErrorString(res)
   Else
      NumberOfKeys = cSubKeys
      MaxKeyNameLength = cbMaxSubKeyLen
   End If
   GetKeyInfo = res
End Function
Note how vbNullString is passed as a parameter for the lpClass parameter. This passes a NULL pointer (pointer with a value of 0) to the API function. The RegQueryInfoKey function is designed so that you can simply pass NULL values for any parameters whose information you do not need. This also improves performance, because Windows won't calculate values you don't ask for.
The Challenge
The GetKeyInfo function shown above is large and inefficient. It forces Windows to calculate information the function does not actually need. Your challenge is to come up with a more efficient solution that uses the RegQueryInfoKey function.

 
< previous page page_55 next page >